//
//  NFSCarsPriceView.m
//  NeedForSpeed
//
//  Created by 王国强 on 15/10/16.
//  Copyright © 2015年 Avenger. All rights reserved.
//

#import "NFSCarsPriceView.h"

#import "BaseHeader.h"

#import "NFSCarPriceCollectionViewCell.h"

@implementation NFSCarsPriceView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self) {
        self =[super initWithFrame:frame];
       // [self getData];
        [self creatCollectionView:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        [self getData];
    }
    return self;
}
//获取数据
-(void)getData
{
    [AFNETWORK getdataWithString:@"http://app.api.autohome.com.cn/autov5.0.0/mall/goods-pm1-c210200-a2.json" block:^(id block) {
        _carsPriceArray =[[NSMutableArray alloc]init];
        for (NSDictionary *dic in [[[block objectForKey:@"result"]objectForKey:@"apricebuycar"]objectForKey:@"list"]) {
            NFSCarsPriceModel *model =[[NFSCarsPriceModel alloc]initWithDictionary:dic];
            [_carsPriceArray addObject:model];
        }
        [_carsPriceCollection reloadData];
    }];
}
-(void)creatCollectionView:(CGRect)frame
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setItemSize:CGSizeMake(SCREEN_WIDTH / 2   , SCREEN_HEIGHT / 3 )];
    [layout setMinimumInteritemSpacing:0.];
    [layout setMinimumLineSpacing:0.];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [layout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    _carsPriceCollection = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
    [_carsPriceCollection setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 2 / 3)];
    [self addSubview:_carsPriceCollection];
    _carsPriceCollection.delegate =self;
    _carsPriceCollection.dataSource =self;
    
    [_carsPriceCollection registerClass:[NFSCarPriceCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NFSCarPriceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NFSCarsPriceModel *model =[_carsPriceArray objectAtIndex:indexPath.row];
    [cell getModel:model];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NFSCarsPriceModel *model =[_carsPriceArray objectAtIndex:indexPath.row];
    [self.NFSCarPrice NFSCarPricesDelegate:model];
    
}
@end
